/*
 * @Descripttion: 
 * @version: 
 * @Author: turingchang
 * @Date: 2020-12-23 09:36:50
 * @LastEditors: turingchang
 * @LastEditTime: 2020-12-23 18:01:06
 */

'use strict';
const enums = require('../../enum/enums');

module.exports = app => {
  const sequelize = app.Sequelize;

  const Job = app.model.define(
    'job', {
      id: {
        type: sequelize.INTEGER(11),
        primaryKey: true,
        autoIncrement: true
      },
      uuid:{
        type: sequelize.STRING(200),
        allowNull: false,
      },
      group_id:{
        type: sequelize.INTEGER(11),
        allowNull: false,
      },
      name:{
        type: sequelize.STRING(200),
        allowNull: false,
      },
      type:{
        type: sequelize.STRING(50),
        allowNull: true,
        defaultValue:enums.job_type.urllink.value,
      },
      cron:{
        type: sequelize.STRING(500),
        allowNull: true,
        defaultValue:'',
      },
      cron_analyzing:{
        type: sequelize.STRING(500),
        allowNull: true,
        defaultValue:'',
      },
      value:{
        type: sequelize.STRING(500),
        allowNull: true,
        defaultValue:'',
      },
      request_type:{
        type: sequelize.STRING(50),
        allowNull: true,
        defaultValue:enums.job_request_type.get.value,
      },
      request_params:{
        type: sequelize.STRING(500),
        allowNull: true,
        defaultValue:'',
      },
      user_id:{
        type: sequelize.INTEGER(11),
        allowNull: true,
        defaultValue:0,
      },
      tag:{
        type: sequelize.STRING(500),
        allowNull: true,
        defaultValue:'',
      },
      frequency:{
        type: sequelize.INTEGER(11),
        allowNull: true,
        defaultValue:0,
      },
      status:{
        type: sequelize.STRING(50),
        allowNull: true,
        defaultValue:enums.job_status.init.value,
      },
      create_time: {
        type:sequelize.DATE,
        defaultValue:sequelize.now,
        get() {
          let t = this.getDataValue('update_time');
          t = t ? app.utils.time.moment(t).format('YYYY-MM-DD HH:mm:ss') :'';
          return t ;
        }
      },
      update_time: {
        type: sequelize.DATE,
        defaultValue:sequelize.now,
        get() {
          let t = this.getDataValue('update_time');
          t = t ? app.utils.time.moment(t).format('YYYY-MM-DD HH:mm:ss') :'';
          return t ;
        }
      },
      delete_time: {
        type: sequelize.DATE,
        defaultValue:null,
        get() {
          let t = this.getDataValue('update_time');
          t = t ? app.utils.time.moment(t).format('YYYY-MM-DD HH:mm:ss') :'';
          return t ;
        }
      },
      next_invocation: {
        type: sequelize.STRING(50),
        defaultValue:null,
        get() {
          let t = this.getDataValue('next_invocation');
          t = t ? app.utils.time.moment(t).format('YYYY-MM-DD HH:mm:ss') :'';
          return t ;
        }
      },
    },
    {
      tableName: 'job', //自定义映射数据库真实表名
      timestamps:false,
      // freezeTableName: true,  //强制表名等于模型名
    },
  );

  return Job;
};